package com.example.scaffolding.ui.main

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.Observer

import org.junit.After
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class PageViewModelTest {
    private lateinit var pageViewModel: PageViewModel

    @get:Rule
    var instantExecutorRule = InstantTaskExecutorRule()

    @Before
    fun setUp() {
        pageViewModel = PageViewModel()
    }

    @After
    fun tearDown() {
    }

    @Test
    fun setIndex_differentPageShouldHaveDifferentTitle() {
        val observer = Observer<String> {}
        pageViewModel.title.observeForever(observer)
        try {
            pageViewModel.setIndex(1)
            val title1 = pageViewModel.title.value
            println("title1: $title1")
            assertFalse(title1.isNullOrEmpty())

            pageViewModel.setIndex(2)
            val title2 = pageViewModel.title.value
            println("title2: $title2")
            assertFalse(title2.isNullOrEmpty())

            assertNotEquals(title1, title2)
        } finally {
            pageViewModel.title.removeObserver(observer)
        }
    }

    @Test
    fun setIndex_invalidPageShouldHaveInvalidTitle() {
        val observer = Observer<String> {}
        pageViewModel.title.observeForever(observer)
        try {
            pageViewModel.setIndex(3)
            val title = pageViewModel.title.value
            println("title: $title")
            assertTrue(title.isNullOrEmpty())
        } finally {
            pageViewModel.title.removeObserver(observer)
        }
    }
}